package net.kleinhenz.norabase.client;

import net.kleinhenz.norabase.shared.model.Battlegroup;
import net.kleinhenz.norabase.shared.model.Rune;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.TextAreaItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.form.fields.CheckboxItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.util.SC;

public class BattlegroupPropertiesDialog extends Window {

	public BattlegroupPropertiesDialog(final Battlegroup<Rune> battlegroup, final BattlegroupTabSet tabSet) {

		setWidth("500px");
		setIsModal(false);
		setShowBody(true);
		setShowFooter(false);
		setShowHeaderBackground(false);
		setShowHeaderIcon(false);
		setShowMinimizeButton(false);
		setShowStatusBar(true);
		setShowTitle(true);
		centerInPage();
		setTitle("Battlegroup Properties");
        setAutoSize(true);  
        setCanDragReposition(true);  
        setCanDragResize(true);  
        setMembersMargin(10);
        
        VLayout layout = new VLayout(10);
        layout.setWidth("100%");
        layout.setMargin(10);
        
        DynamicForm dynamicForm = new DynamicForm();

        final TextItem nameField = new TextItem("name", "Battlegroup&nbsp;Name");
        nameField.setRequired(true);
        nameField.setValidators(new NullValidator());
        nameField.setValue(battlegroup.getName());
        nameField.setWidth("100%");
        
        final TextAreaItem descField = new TextAreaItem("description", "Description");
        descField.setRequired(false);
        descField.setValue(battlegroup.getDesc());
        descField.setWidth("100%");

        final TextAreaItem stratField = new TextAreaItem("strategy", "Strategy");
        stratField.setRequired(true);
        stratField.setValue(battlegroup.getStrat());
        stratField.setWidth("100%");

        final CheckboxItem checkboxItem = new CheckboxItem("public", "Public (Battlegroup is read-only visible to other users)");
        checkboxItem.setValue(battlegroup.isPublicBattlegroup());
        
        dynamicForm.setFields(new FormItem[] { nameField, descField, stratField, checkboxItem});
        
        layout.addMember(dynamicForm);
        
        HLayout btnLayout = new HLayout(10);
        btnLayout.setAlign(Alignment.RIGHT);
        btnLayout.setDefaultLayoutAlign(VerticalAlignment.CENTER);
        btnLayout.setMembersMargin(10);
        
        Button btnSave = new Button("Save");
        btnSave.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				battlegroup.setName((String)nameField.getValue());
				battlegroup.setDesc((String)descField.getValue());
				battlegroup.setStrat((String)stratField.getValue());
				Object v = checkboxItem.getValue();
				battlegroup.setPublicBattlegroup((Boolean)v);
				BattlegroupServiceAsync service = GWT.create(BattlegroupService.class);
				service.updateBattlegroup(battlegroup, new AsyncCallback<Void>() {
					public void onFailure(Throwable caught) {
						SC.say("An error occured while updating battlegroup info on the server. Please try again.");
					}
					@Override
					public void onSuccess(Void result) {
						tabSet.setCurrentTabTitle(battlegroup.getName());
						BattlegroupPropertiesDialog.this.destroy();
					}
				});
			}
		});
        btnLayout.addMember(btnSave);
        
        Button btnCancel = new Button("Cancel");
        btnCancel.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				BattlegroupPropertiesDialog.this.destroy();
			}
		});
        btnLayout.addMember(btnCancel);

        layout.addMember(btnLayout);
        addItem(layout);
	}
}
